<?php
/**
 *  Svn Log 类
 * @author Alan.H
 */

require_once dirname(__FILE__) . '/Command/Log.php';
require_once dirname(__FILE__) . '/Command/Info.php';
require_once dirname(__FILE__) . '/Command/Cat.php';

require_once dirname(__FILE__) . '/../diff/Diff.php';

class SvnLog {
    /**
     * SVN仓库URL地址
     * @var null
     */
    protected $url = null;

    /**
     * SVN仓库Root
     * @var null
     */
    protected $root = null;

    /**
     * 登录SVN仓库的账号
     * @var null
     */
    protected $username = null;

    /**
     * 登录SVN仓库的密码
     * @var null
     */
    protected $password = null;

    /**
     * 运行时文件目录
     * @var null
     */
    protected $runtime = null;

    /**
     * 版本号
     * @var null
     */
    protected $revision = null;

    /**
     * @param $url
     * @param $username
     * @param $password
     */
    public function __construct($url = null, $username = null, $password = null) {
        $url && $this->setUrl($url);
        $username && $this->setUsername($username);
        $password && $this->setPassword($password);
    }

    /**
     * @throws Exception
     */
    public function run() {
        if ( ! $this->url) {
            throw new InvalidArgumentException('Url is empty!');
        }

        if ( ! $this->revision) {
            throw new InvalidArgumentException('Revision is empty!');
        }

        // svn info
        $info= new Info($this);
        $info->execCmd();

        $this->setRoot($info->getParam('root'));

        // svn log
        $log = new Log($this);
        $log->setRevision($this->revision);
        $log->execCmd();

        // svn cat
        $cat   = new Cat($this);
        $params= $log->getParam();
        foreach ($params as $revision => &$items) {
            foreach ($items['paths'] as &$path) {
                if ($path['kind'] == 'file') {
                    $cat->setPath($path['path']);
                    $cat->setRevision($revision);
                    $cat->execCmd();
                    $path['new'] = $cat->getFile();

                    $cat->setRevision($revision - 1);
                    $cat->execCmd();
                    $path['old'] = $cat->getFile();
                }
            }
        }
        return $params;
    }

    /**
     * @param $url
     * @return $this
     */
    public function setUrl($url) {
        $this->url = $url;
        return $this;
    }

    public function getUrl() {
        return $this->url;
    }

    /**
     * @param $username
     * @return $this
     */
    public function setUsername($username) {
        $this->username = $username;
        return $this;
    }

    public function getUsername() {
        return $this->username;
    }

    /**
     * @param $password
     * @return $this
     */
    public function setPassword($password) {
        $this->password = $password;
        return $this;
    }

    public function getPassword() {
        return $this->password;
    }

    /**
     * @param $runtime
     * @return $this
     */
    public function setRuntime($runtime) {
        $this->runtime = $runtime;
        return $this;
    }

    public function getRuntime() {
        return $this->runtime;
    }

    /**
     * @param $revision
     * @return $this
     */
    public function setRevision($revision) {
        $this->revision = $revision;
        return $this;
    }

    public function getRevision() {
        return $this->revision;
    }

    /**
     * @param $root
     * @return $this
     */
    public function setRoot($root) {
        $this->root = $root;
        return $this;
    }

    public function getRoot() {
        return $this->root;
    }
}